package com.travel.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.travel.bean.Picture;

import com.travel.bean.Strategy;
import com.travel.service.HotSpotService;
import com.travel.service.StrategyService;
import com.travel.utils.PageHelper;
import com.travel.utils.RandomUtils;
import com.travel.utils.UploadFiles;

@Controller
@RequestMapping("/strategy")
public class StrategyController {
	@Resource(name="strategyServiceImpl")
	private StrategyService strategyServiceImpl;
	/**
	 * 展示热门景点
	 */
	@Resource(name="hostSpotserviceImpl")
	private HotSpotService hostSpotserviceImpl;
	/**
	 * 跳转到攻略详情页展示攻略详情
	 * @param id
	 * @param map
	 * @return
	 */
	@RequestMapping("/toview/{id}")
	public String toView(@PathVariable Integer id, Map map){
		Strategy strategy=strategyServiceImpl.getEntryById(id);
		map.put("strategy", strategy);
		//页面底部的热门景点推荐
		List<Map> hotSpots=hostSpotserviceImpl.indexHotSpot();
		map.put("hotSpots",hotSpots);
		return "jsp/strategydetial";
	}
	/**
	 * 展示热点攻略
	 * @param map
	 * @return
	 */
	@RequestMapping("/showhotline.do")
	public String showPage(Map map){
		List<Map>strategys=strategyServiceImpl.queryByPageIndex(null, 1, 3);
		map.put("strategys", strategys);
		return "jsp/index";
	}
	
	
	//后台数据操作
	/**
	 * 跳转到添加攻略页面
	 * @return
	 */
	@RequestMapping("/toaddstrategy.html")
	public String toAddStrategy(){
		return "admin/addstrategy";
	}
 	/**
	 * 跳转到展示攻略详情
	 * @return
	 */
	@RequestMapping("/toviewstrategy.html")
	public String toViewStrategy(){
		return "admin/viewstrategy";
	}
	/**
	 * 异步加载数据到页面
	 * @param limit 页数据条数
	 * @param page 页码
	 * @return
	 */
	@RequestMapping("/viewstrategy.do")
	@ResponseBody
	public Map viewStrategy(Integer limit,Integer page){
		Map map=new HashMap();
		PageHelper pages=strategyServiceImpl.queryByPageList(map, page, limit);
		map.put("code", 0);
		map.put("count", pages.getTotalPageCount());
		map.put("data", pages.getList());
		return map;
	}
	/**
	 * 上传多个图片
	 * @param attachs
	 * @param request
	 * @return
	 */
	@RequestMapping("/upload.do")
	@ResponseBody
	public Map addPicture(@RequestParam("file")MultipartFile attach,HttpSession session){
		Map map =new HashMap();
		System.out.println(attach.getName());
		MultipartFile[] attachs=new MultipartFile[]{attach};
		List<Map> filesInfo=UploadFiles.UploadPics(attachs, "strategy", session);
		//存放图片集合
		Object obsips=session.getAttribute("s1ps");
		Set s1ps;
		if(obsips==null){
		 s1ps=new HashSet();
		}else{
			s1ps=(Set) obsips;
		}
		//存放图片id
		Object pic1id=session.getAttribute("picid1");
		Integer picid1;
		if(pic1id==null){
		 picid1=RandomUtils.RandomNumber(4);
		 session.setAttribute("picid1", picid1);
		}else{
			picid1=(Integer) pic1id;
		}
		//存放图片序号
		Object objsort=session.getAttribute("sort");
		Integer sort=1;
		if(objsort==null){
		 session.setAttribute("sort", 1);
		}else{
			sort=(Integer)objsort;
			session.setAttribute("sort", sort+1);
		}
		boolean flag=true;
		try {
			for (Map fileMap : filesInfo) {
				
			}
			session.setAttribute("s1ps", s1ps);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			flag=false;
			e.printStackTrace();
		}
		
		if(flag){
			map.put("msg", "上传成功！");
			map.put("picid1",picid1);
		}else{
			map.put("msg", "上传失败！");
			map.put("picid1",picid1);
		}
		
		return map;
	}
	
	@RequestMapping("/addstrategy.do")
	public String addStrategy(Strategy strategy,HttpSession session){
		strategy.setCreateTime(new Date());
		
		
		return "admin/index";
	}
}
